<template>
    <view class="nova-list">
        <text class="li-section">基本信息</text>
        <view class="nova-list">
            <nova-pref class="li-pref" title="头像" placeholder="" :arrow="true">
                <view class="picker" @click="pickImg('photo')">
                    <image v-if="info.photo || role == 3" class="profile" :src="info.photo"></image>
                    <!-- <text class="placeholder" v-else>上传真实头像</text> -->
                    <view v-else class="nova-column" style="align-items: flex-end;"><open-data class="profile" type="userAvatarUrl"></open-data></view>
                </view>
            </nova-pref>
            <nova-pref class="li-pref" title="姓名" placeholder="请填写真实姓名" :arrow="true" type="input" name="name" :value="info.name" @input="input"></nova-pref>

            <nova-pref
                class="li-pref"
                title="身份证号"
                placeholder="请填写身份证号码"
                :arrow="true"
                name="card_id"
                :value="info.card_id"
                type="input"
                inputType="text"
                @input="input"
                @blur="blur"
            ></nova-pref>
            <nova-pref class="li-pref" title="性别" placeholder="请选择性别" :arrow="true">
                <radio-group @change="changeSex">
                    <label>
                        <radio value="女" class="" :checked="info.sex == '女'"></radio>
                        女
                    </label>
                    <label>
                        <radio value="男" :checked="info.sex == '男'"></radio>
                        男
                    </label>
                </radio-group>
            </nova-pref>
            <nova-pref class="li-pref" title="出生日期" placeholder="" :arrow="true">
                <picker class="picker" mode="date" :value="info.birthday" @change="changeBirth">
                    <text v-if="info.birthday">{{ info.birthday }}</text>
                    <text v-else class="placeholder">请选择出生日期</text>
                </picker>
            </nova-pref>
            <nova-pref class="li-pref" title="属相" placeholder="" :arrow="true">
                <picker class="picker" mode="selector" :value="animals.indexOf(info.zodiac) >= 0 ? animals.indexOf(info.zodiac) : ''" @change="changeAnimal" :range="animals">
                    <text v-if="info.zodiac">{{ info.zodiac }}</text>
                    <text v-else class="placeholder">请选择您的属相</text>
                </picker>
            </nova-pref>
            <nova-pref class="li-pref" title="星座" placeholder="" :arrow="true">
                <picker
                    class="picker"
                    mode="selector"
                    :value="constellations.indexOf(info.constellation) >= 0 ? constellations.indexOf(info.constellation) : ''"
                    @change="changeXZ"
                    :range="constellations"
                >
                    <text v-if="info.constellation">{{ info.constellation }}</text>
                    <text v-else class="placeholder">请选择您的星座</text>
                </picker>
            </nova-pref>
            <nova-pref class="li-pref" title="籍贯" placeholder="" :arrow="true">
                <picker class="picker" mode="selector" :value="places.indexOf(info.place) >= 0 ? places.indexOf(info.place) : ''" @change="changeJG" :range="places">
                    <text v-if="info.place">{{ info.place }}</text>
                    <text v-else class="placeholder">请选择您的籍贯</text>
                </picker>
            </nova-pref>
            <nova-pref class="li-pref" title="民族" placeholder="" :arrow="true">
                <picker class="picker" mode="selector" :value="nations.indexOf(info.nation) >= 0 ? nations.indexOf(info.nation) : ''" @change="changeMZ" :range="nations">
                    <text v-if="info.nation">{{ info.nation }}</text>
                    <text v-else class="placeholder">请选择您的民族</text>
                </picker>
            </nova-pref>
            <nova-pref
                class="li-pref"
                title="工作经验"
                placeholder="请填写工作经验"
                :arrow="true"
                name="workYear"
                type="input"
                inputType="number"
                :value="info.workYear"
                @input="input"
                unit="年"
            ></nova-pref>
            <nova-pref
                class="li-pref"
                title="联系方式"
                placeholder="请填写联系方式"
                :arrow="true"
                name="telephone"
                type="input"
                inputType="number"
                :value="info.telephone"
                @input="input"
            ></nova-pref>
<!--            <nova-pref class="li-pref" title="当前住址" placeholder="请选择所在住址" :value="info.village" :arrow="true" @click.native="goMap"></nova-pref> -->
            <choose-location class="li-pref" title="当前住址" placeholder="请选择所在住址" :loc="loc" @change="goMap" />
            <nova-pref class="li-pref" title="求职状态" placeholder="" :arrow="false">
                <radio-group @change="changeState">
                    <label>
                        <radio value="1" :checked="info.work_status == 1"></radio>
                        在单上
                    </label>
                    <label>
                        <radio value="2" :checked="info.work_status == 2"></radio>
                        急找工作
                    </label>
                </radio-group>
            </nova-pref>
        </view>
<!--        <text class="li-section">证件信息</text>
        <view class="nova-row-center" style="justify-content: space-between;">
            <view class="id-box" @click="pickImg('cardid_img_just')">
                <block v-if="info.cardid_img_just"><image class="id" :src="info.cardid_img_just"></image></block>
                <block v-else>
                    <image class="bg" src="../../static/jn/id1.png"></image>
                    <image class="plus" src="../../static/jn/add_c.png"></image>
                </block>
            </view>
            <view class="id-box" @click="pickImg('cardid_img_back')">
                <block v-if="info.cardid_img_back"><image class="id" :src="info.cardid_img_back"></image></block>
                <block v-else>
                    <image class="bg" src="../../static/jn/id2.png"></image>
                    <image class="plus" src="../../static/jn/add_c.png"></image>
                </block>
            </view>
        </view> -->
        <view class="nova-row-center" style="background-color: #f4f4f4;">
            <text class="li-section">求职意向</text>
            <text style="flex: 1;"></text>
            <text class="li-section add" @click="addSkill">添加新意向</text>
        </view>
        <view class="nova-list" v-if="skills">
            <nova-pref class="li-pref pref-icon" :arrow="false" v-for="(item, index) in skills" :key="index" :title="getTypeName(item)">
                <view class="nova-row-center">
                    <view class="del nova-row-center" @click="doDelete(item)">
                        <image src="../../static/jn/del.png"></image>
                        <text>删除</text>
                    </view>
                    <view class="mod nova-row-center" @click="doEdit(item)">
                        <image src="../../static/jn/mod.png"></image>
                        <text>编辑</text>
                    </view>
                </view>
            </nova-pref>
        </view>
        <view style="background-color: #F2F2F2; padding-bottom: 100rpx;">
            <button class="save" @click="save">保存简历</button>
            <button class="save" @click="refreshResume" v-if="role == 1">刷新简历</button>
        </view>
    </view>
</template>

<script>
import novaPref from '../../components/nova/pref.vue';
import conf from '../../common/conf.js';
import ChooseLocation from './choose-location.vue';
export default {
    name: 'p-resume',
    components: {
        novaPref,
        ChooseLocation
    },
    props: {
        type: {
            type: Number,
            value: 1
        }
    },
    data() {
        return {
            role: this.$global.role,
            types: undefined,
            animals: conf.animals,
            sexlist: conf.sexlist,
            constellations: conf.constellations,
            nations: conf.nations,
            places: conf.provinces2,
            info: {
                birthday: 0,
                cardid_img_back: '',
                nation: '',
                zodiac: '',
                sex: '女',
                photo: '',
                telephone: '',
                cardid_img_just: '',
                type: 1,
                card_id: '',
                work_status: 2,
                constellation: '',
                name: '',
                place: '',
                workYear: '',
                nurse_id: '',
                village: '',
                resume_id: '',
                latitude: '',
                longitude: ''
            },
            skills: [],
            loc: {
                latitude: '',
                longitude: '',
                name: '',
                address: '',
                areaid: ''
            }
        };
    },
    onShow(e) {
        // let pages = getCurrentPages();
        // let currPage = pages[pages.length-1];
        // if(currPage.data.info.village==undefined || currPage.data.info.village==''){
        // }else{
        // 	this.info.village = currPage.data.info.village
        // 	this.loc = {
        // 		latitude: currPage.data.loc.latitude,
        // 		longitude: currPage.data.loc.longitude,
        // 		name: currPage.data.loc.name,
        // 		address: currPage.data.loc.address,
        // 		areaid:currPage.data.loc.areaid,
        // 	}
        // }
    },
    created() {
        if (this.$global.role == 1) {
            this.info.type = 1;
            this.loadData();
        } else if (this.$global.role == 3) {
            this.info.type = 2;
        }
        // #ifdef H5
        if (!this.info.resume_id) {
            this.info.village = '北京东路64号';
        }
        // #endif
        console.log('$on skillUpdated');
        uni.$on('skillUpdated', this.skillUpdated);
    },
    destroyed() {
        console.log('$off skillUpdated');
        uni.$off('skillUpdated');
    },
    methods: {
        skillUpdated() {
            console.log('event skillUpdated');
            this.loadData();
        },
        async loadData() {
            let data = {
                type: this.role
            };
            if (this.role == 3) {
                data.resume_id = this.info.resume_id;
                data.type = 3;
            }
            let res = await this.$request.post({
                url: '/aunt/resumeSkill',
                data: data
            });
            if (res.resumeSkill) {
                if (!res.resumeSkill.birthday) {
                    res.resumeSkill.birthday = 0;
                }
                // if (res.workyear) {
                //     this.info.workYear = res.workyear;
                // }
                if (res.resumeSkill.latitude) {
                    this.loc.latitude = res.resumeSkill.latitude;
                }
                if (res.resumeSkill.longitude) {
                    this.loc.longitude = res.resumeSkill.longitude;
                }
                if (res.resumeSkill.village) {
                    this.loc.name = res.resumeSkill.village
                }
                this.info = res.resumeSkill || {};
            } else {
                let ub = this.$global.userbasic
                if (ub && this.role == 1) {
                    for(let k in ub) {
                        if (this.info.hasOwnProperty(k) && ub[k]) {
                            this.info[k] = ub[k]
                        }
                    }
                    if (ub.latitude) {
                        this.loc.latitude = ub.latitude;
                    }
                    if (ub.longitude) {
                        this.loc.longitude = ub.longitude;
                    }
                    if (ub.headimg) {
                        this.info.photo = ub.headimg
                    }
                    if (ub.contact || ub.phone) {
                        this.info.telephone = ub.contact || ub.phone;
                    }
                }
            }
            this.skills = res.skillList || [];
            
        },
        async loadData2() {
            let that = this;
            if (!this.info.resume_id) {
                let res = await this.$request.post({
                    url: '/resume/resumeCompany',
                    data: {
                        cardId: this.info.card_id
                    },
                    loadingTip: '正在检查身份证...',
                    toastError: false
                });
                if (res.resume) { // 
                    let tmp = res.resume;
                    if (!res.resume.birthday) {
                        res.resume.birthday = 0;
                    }
                    if (res.resume.latitude) {
                        this.loc.latitude = res.resume.latitude;
                    }
                    if (res.resume.longitude) {
                        this.loc.longitude = res.resume.longitude;
                    }
                    if (res.resume.village) {
                        this.loc.name = res.resume.village;
                    }
                    if (this.info.photo) {
                        delete res.resume.photo
                    }
                    if (this.info.name) {
                        delete res.resume.name
                    }
                    if (this.info.telephone) {
                        delete res.resume.telephone
                    }
                    Object.assign(this.info, res.resume);
                } else { // first 
                    let y = this.info.card_id.slice(6,10);
                    let m = this.info.card_id.slice(10,12);
                    let d = this.info.card_id.slice(12,14);
                    this.info.birthday = `${y}-${m}-${d}`
                    let wnl = await this.$request.post({
                        // url: 'https://cdn.ieclipse.cn/wnl/lunar?date=' + this.info.birthday,
                        // skipInterceptorResponse: true,
                        // business: null,
                        url: '/aunt/forward',
                        data: {
                            date: this.info.birthday
                        },
                        toastError: false,
                        loadingTip: '正在检查身份证...',
                    })
                    // if (wnl.code == 10001) {
                    //     wnl.code = 0;
                    // }
                    // if (wnl.code == 0) 
                    {
                        //wnl = wnl.data;
                        this.info.zodiac = wnl.cAnimal;
                        this.info.constellation = wnl.constellation;
                        console.log(this.info);
                    }
                }
                this.skills = res.skillResume || [];
            }
        },
        pickImg(key) {
            var that = this;
            uni.chooseImage({
                count: 1,
                success: function(res) {
                    console.log(res);
                    let path = res.tempFilePaths[0];
                    //that.info[key] = path;
                    that.upload(key, path);
                }
            });
        },
        async upload(key, f) {
            var that = this;
            let res = await this.$request.upload({
                url: '/user/uploadPic',

                filePath: f,
                name: 'file',
                loadingTip: '上传中，请稍候',
                formData: {}
            });
            console.log(res);
            let url = res[0].url;
            console.log(key, url);
            that.info[key] = url;
            return;
        },
        changeAnimal(e) {
            this.info.zodiac = this.animals[e.detail.value];
        },
        changeBirth(e) {
            this.info.birthday = e.detail.value;
        },
        changeSex(e) {
            this.info.sex = e.detail.value;
        },
        changeXZ(e) {
            this.info.constellation = this.constellations[e.detail.value];
        },
        changeJG(e) {
            this.info.place = this.places[e.detail.value];
        },
        changeMZ(e) {
            this.info.nation = this.nations[e.detail.value];
        },
        changeState(e) {
            this.info.work_status = e.detail.value;
        },
        input(e) {
            let k = e.target.name;
            this.info[k] = e.detail.value;
        },
        blur(e) {
            console.log(e);
            let k = e.target.name;
            if (k === 'card_id') {
                this.info[k] = e.detail.value;
            }
            if (this.info[k].length !== 18) {
                this.$toast('身份证号码位数不对');
            } else if (!/\d{17}[\dxX]/.test(this.info[k])) {
                this.$toast('身份证号码格式不对');
            } else {
                let l7 = parseInt(this.info.card_id.slice(16, 17));
                console.log('no 17 is ' + l7);
                if (l7 % 2 != 0) {
                    this.info.sex = '男';
                } else {
                    this.info.sex = '女';
                }
                this.loadData2();
            }
        },
        async save() {
            let err;
            if (!this.info.photo) {
                //err = '请选择头像';
            } else if (!this.info.name) {
                err = '请输入姓名';
            } else if (!this.info.card_id) {
                err = '请输入正确的身份证号码';
            } else if (!this.info.zodiac) {
                err = '请选择属相';
            } else if (!this.info.constellation) {
                err = '请选择星座';
            } else if (!this.info.place) {
                err = '请选择籍贯';
            } else if (!this.info.nation) {
                err = '请选择民族';
            } else if (!this.info.telephone) {
                err = '请输入联系方式';
            } else if (!this.$isMobile(this.info.telephone)) {
                err = '请输入正确的联系方式';
            }
            if (err) {
                this.$toast(err);
                return;
            }
            console.log('post', this.info);
            let res = await this.$request.post({
                url: '/resume/resume',
                data: this.info,
                loadingTip: '正在提交修改，请稍候...'
            });
            this.info = res;
            this.$toast('简历保存成功');
            this.$getUserBasic()
        },
        async refreshResume() {
            console.log('post', this.info);
            let res = await this.$request.post({
                url: '/resume/resume',
                data: this.info
            });
            this.info = res;
            this.$toast('简历刷新成功');
        },
        goMap(res) {
            console.log('change loc', res);
            let _this = this;
            _this.info.village = res.name;
            _this.loc = {
                latitude: res.latitude,
                longitude: res.longitude,
                name: res.name,
                address: res.address
            };
            this.info.latitude = res.latitude;
            this.info.longitude = res.longitude;
        },
        addSkill() {
            if (!this.info.resume_id) {
                this.$toast('请先保存简历');
                return;
            }
            uni.setStorageSync('resume_id', this.info.resume_id);
            uni.navigateTo({
                url: '../c/p-publish-category?delegate=1&resume_id=' + this.info.resume_id
            });
        },
        doDelete(item) {
            let that = this;
            uni.showModal({
                content: '确认删除此技能？',
                success(res) {
                    if (res.confirm) {
                        that.apiDelete(item);
                    }
                }
            });
        },
        async apiDelete(item) {
            let res = await this.$request.post({
                url: '/aunt/skillDelete',
                data: {
                    skill_id: item.skill_id,
                    resume_id: item.resume_id
                }
            });
            this.$toast('技能删除成功');
            this.loadData();
        },
        doEdit(item) {
            uni.navigateTo({
                url: '../c/p-publish?skill_id=' + item.skill_id
            });
        },
        getTypeName(skill) {
            let type = skill.type;
            if (typeof type === 'string') {
                type = parseInt(type);
            }
            if (type > 0 && type < conf.ayTypes.length) {
                return conf.ayTypes[type];
            }
            return '其它';
        }
    }
};
</script>

<style lang="scss" scoped>
.radio {
    /deep/ .uni-radio-input {
        width: 24rpx;
        height: 24rpx;
        &:checked:before {
            width: 18rpx;
        }
    }
}
.radiox {
    margin-left: 12rpx;
}
.picker {
    width: 100%;
    text-align: right;
}
.profile {
    width: 64rpx;
    height: 64rpx;
    border-radius: 32rpx;
    margin-right: 10rpx;
    overflow: hidden;
}
.id-box {
    position: relative;
    image.id {
        width: 298rpx;
        height: 184rpx;
        margin: 30rpx 40rpx;
    }
    image.bg {
        width: 298rpx;
        height: 184rpx;
    }
    image.plus {
        width: 40rpx;
        height: 40rpx;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-left: -20rpx;
        margin-top: -20rpx;
    }
}
.add {
    color: #337fe7;
    position: relative;
    margin-right: 34rpx;
    &:after {
        content: '+';
        margin-left: 10rpx;
        font-weight: 800;
    }
}
.pref-icon {
    .icon {
        width: 37rpx;
        height: 37rpx;
        margin-right: 27rpx;
    }
    .del {
        color: $uni-color-primary;
        font-size: 26rpx;
        margin-right: 25rpx;
        image {
            width: 16rpx;
            height: 16rpx;
        }
    }
    .mod {
        color: #337fe7;
        font-size: 26rpx;
        margin-right: 25rpx;
        image {
            width: 22rpx;
            height: 22rpx;
        }
    }
}
.save {
    background-color: white;
    height: 100rpx;
    line-height: 100rpx;
    color: $uni-color-primary;
    margin: 30rpx 0;
}
</style>
